home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12534 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: RAND_MAX
  5. Date: Mon, 01 Apr 96 14:56:10 GMT
  6. Organization: none
  7. Message-ID: <828370570snz@genesis.demon.co.uk>
  8. References: <4jnr55$e6l@skivs.ski.org>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jnr55$e6l@skivs.ski.org> gt@ns.oon.or.jp "Gemini Thunder" writes:
  15.  
  16. >I have a (stupid) question / observation about RAND_MAX.
  17. >
  18. >K&R2 says:
  19. >  "rand returns a a pseudo-random integer in the range 0 to RAND_MAX,
  20. >which is at least 32767"
  21. >
  22. >It looks like RAND_MAX is not required to be the same as any other
  23. >limit (such as MAX_INT, etc.), that is fine, but why?  
  24. >Wouldn't it be easier if RAND_MAX == MAX_INT or some other value?  
  25.  
  26. RAND_MAX is a characteristic of the algorithm used, not simply the types
  27. supported by the implementation.
  28. >
  29. >The reason I say this is how can you be sure your array will hold a
  30. >RAND_MAX sized integer, without some checking before hand, if RAND_MAX
  31. >is out there doing its own thing?
  32.  
  33. rand() returns int so you can hold the value in an int and RAND_MAX can't be
  34. greater than INT_MAX. However it is guaranteed to be at least 32767.
  35.  
  36. >Or does ANSI pin it down (in which case why have a RAND_MAX #defined),
  37. >or am I just worked up over nothing and overlooking something obvious?
  38.  
  39. Store it in an int.
  40.  
  41. -- 
  42. -----------------------------------------
  43. Lawrence Kirby | fred@genesis.demon.co.uk
  44. Wilts, England | 70734.126@compuserve.com
  45. -----------------------------------------
  46.